package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Test;

public class ExecutorsTest {
	
	public static int i = 0;

	/**
	 * 测试线程池的任务执行顺序
	 */
	@Test
	public void executeOrder(){
		
		ExecutorService service = Executors.newFixedThreadPool(4);
		service.execute(new Runnable() {
			
			@Override
			public void run() {
				
				System.out.format("线程：%d,执行第：%d个任务开始\n", Thread.currentThread().getId(), 1);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.format("线程：%d,执行第：%d个任务结束\n\n", Thread.currentThread().getId(), 1);
			}
		});
		service.execute(new Runnable() {
			
			@Override
			public void run() {
				
				System.out.format("线程：%d,执行第：%d个任务开始\n", Thread.currentThread().getId(), 2);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.format("线程：%d,执行第：%d个任务结束\n\n", Thread.currentThread().getId(), 2);
			}
		});
		service.execute(new Runnable() {
			
			@Override
			public void run() {
				
				System.out.format("线程：%d,执行第：%d个任务开始\n", Thread.currentThread().getId(), 3);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.format("线程：%d,执行第：%d个任务结束\n\n", Thread.currentThread().getId(), 3);
			}
		});
		service.execute(new Runnable() {
			
			@Override
			public void run() {
				
				System.out.format("线程：%d,执行第：%d个任务开始\n", Thread.currentThread().getId(), 4);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.format("线程：%d,执行第：%d个任务结束\n\n", Thread.currentThread().getId(), 4);
			}
		});
		service.execute(new Runnable() {
			
			@Override
			public void run() {
				
				System.out.format("线程：%d,执行第：%d个任务开始\n", Thread.currentThread().getId(), 5);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.format("线程：%d,执行第：%d个任务结束\n\n", Thread.currentThread().getId(), 5);
			}
		});
		service.execute(new Runnable() {
			
			@Override
			public void run() {
				
				System.out.format("线程：%d,执行第：%d个任务开始\n", Thread.currentThread().getId(), 6);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.format("线程：%d,执行第：%d个任务结束\n\n", Thread.currentThread().getId(), 6);
			}
		});
		
		try {
			Thread.sleep(9999999);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
}
